package developer.zyk.global.user.helper;

import cn.hutool.core.collection.CollUtil;
import developer.zyk.global.user.bo.UserInfo;
import developer.zyk.global.user.dao.entity.ChatUserDO;
import developer.zyk.global.user.dao.mappers.ChatUserMapper;
import developer.zyk.global.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhangyongkang
 * @time 2025/5/16 14:57
 * @description
 */
@Component
public class UserInfoHelper {

    private static ChatUserMapper chatUserMapper;

    @Autowired
    public void setChatUserDO(ChatUserMapper chatUserMapper) {
        UserInfoHelper.chatUserMapper = chatUserMapper;
    }


    public static UserInfo getUserInfoById(Long userId) {
        AssertUtil.assertNonNull(userId, "用户ID不可为null");
        ChatUserDO chatUserDO = chatUserMapper.selectById(userId);
        AssertUtil.assertNonNull(userId, "信息为null,userId:" + userId);
        return UserInfo.of(chatUserDO);
    }

    public static Map<Long, UserInfo> getUserInfoMapByIds(List<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return new HashMap<>();
        }
        return chatUserMapper.selectByIds(ids)
                .stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toMap(ChatUserDO::getId, UserInfo::of, (a, b) -> b));
    }
}
